<?php ob_start();if(!isset($_SESSION)){
  session_start();$message="";
}

require_once('../../../include/inc_config.php');

$bool = TRUE ;
$col_rs_tarif = 1 ;
$iIdentifiantPersonne = getHttpVar('Identifiant_personne', 0) ;
$iIdentifiantContrat  = getHttpVar('Identifiant_contrat', 0) ;

$oContrat = new dataObject_contrat($iIdentifiantContrat) ;
$_SESSION['num_contrat']  = $oContrat->_NUMERO ; 


if(array_key_exists('modepaiement', $_POST))
{
    $_SESSION['modepaiement'] = $_POST['modepaiement'] ;
}

$requete1=mysql_query("select * from contrat,garantiesouscrite where contrat.Identifiant_contrat='".$iIdentifiantContrat."' and contrat.Identifiant_contrat=garantiesouscrite.Identifiant_contrat ") or die(mysql_error());
if($requete1 && mysql_num_rows($requete1))
{
    $reponse1 = mysql_fetch_array($requete1) ;
    
    $col_rs_tarif = $reponse1['Identifiant_formule'] ; 
    $_SESSION['modepaiement'] = $reponse1['Mode_paiement'] ;
    
    // --- si on a changer de mode de paiement
	// --- et que l'ancien mode est prelèvement
	// --- et qu'il y a encore un rib actif rattacher à ce contrat
	if( array_key_exists('modepaiement', $_POST))
	{
		$zSql  = " SELECT contrat.Identifiant_rib FROM contrat" ;
		$zSql .= " INNER JOIN rib ON rib.Identifiant_rib = contrat.Identifiant_rib" ;
		$zSql .= " WHERE contrat.Identifiant_contrat='" . $iIdentifiantContrat . "'" ;
		$zSql .= " AND rib.Etat_rib = '" . ETAT_RIB_ACTIF . "'" ;
		
		$oRes = mysql_query ($zSql) ;

		if ( $reponse1['Mode_paiement'] != $_POST['modepaiement'] && $reponse1['Mode_paiement'] == MODE_PAIEMENT_PRELEVEMENT )
		{
			if (mysql_num_rows($oRes))
			{
				$bool = FALSE ;
				$message = $message. colorer("Vous devez supprimer le RIB du contrat","false");
			}
		}
		elseif ($reponse1['Mode_paiement'] != $_POST['modepaiement'] && $_POST['modepaiement'] == MODE_PAIEMENT_PRELEVEMENT)
		{
			if (!mysql_num_rows($oRes))
			{
				$bool = FALSE ;
				$message = $message. colorer("Vous devez rattacher un RIB au contrat","false");
			}
		}
	}
}

//if(isset($_POST['MM_insert']))$vide=!empty($_POST['datereglement']) && !empty($_POST['refreglement']) && !empty($_POST['montantecheanceregle'])   ;
//if(isset($_POST['MM_insert']))$bool=datevalide($_POST['datereglement']) && datevalider($_POST['datereglement']) && verifLibelle('refreglement') && verifNumeric($_POST['montantecheanceregle'])  && ( !empty($_POST['montantassure']) && verifNumeric($_POST['montantassure']))  &&  (!empty($_POST['montantdroitentre']) && verifNumeric($_POST['montantdroitentre'])) ;

mysql_select_db($database_microassur, $microassur);
$query_rs_tarif = sprintf("SELECT * FROM tarif WHERE Identifiant_formule = %s", GetSQLValueString($col_rs_tarif, "int"));
$rs_tarif = mysql_query($query_rs_tarif, $microassur) or die(mysql_error());
$row_rs_tarif = mysql_fetch_assoc($rs_tarif);
$totalRows_rs_tarif = mysql_num_rows($rs_tarif); 
// if( $totalRows_rs_tarif>0 && isset($_POST['MM_insert']) && $_POST['MM_insert']=='form1'  )
if( $totalRows_rs_tarif>0 )
{
		$req=mysql_query("select * from contrat where Numero_contrat='".$_SESSION['num_contrat']."'");
		$rep=mysql_fetch_array($req);
		$freqpaiment=$rep['Frequence_paiement'];
		if($freqpaiment=='A')$_SESSION['tarif']= 0;
		if($freqpaiment=='M')$_SESSION['tarif']= $row_rs_tarif['Taux_echeance_mensuel'];
		if($freqpaiment=='T')$_SESSION['tarif']=$row_rs_tarif['Taux_echeance_trimestriel'];
		if($freqpaiment=='S')$_SESSION['tarif']=$row_rs_tarif['Taux_echeance_semestriel'];
		
	if($row_rs_tarif['Type_tarif']=='M')
	{	//2.1
		$montantcotisation=ceil($row_rs_tarif['Montant_tarif_base']+ ($row_rs_tarif['Montant_tarif_base']* $_SESSION['tarif']));
	}
	elseif($row_rs_tarif['Type_tarif']=='T' && !empty($_POST['montantassure']))
	{
				//2.2
				$montant_int=$row_rs_tarif['Taux_tarif_base'] * $_POST['montantassure']; 
				$montantcotisation=ceil($montant_int + ($montant_int * $_SESSION['tarif']));
	}
	//if($_POST['montantcotis']!=$montantcotisation)$message.="<br>Inégalité montant cotisation";
	if($row_rs_tarif['Type_tarif']=='T' && !empty($_POST['montantassure']))
	{
		//3
		$montant_int=$row_rs_tarif['Taux_tarif_base'] * $_POST['montantassure']; 
		$montantfraisecheance=ceil($montant_int * $_SESSION['tarif']);
		//if($_POST['fraisecheance']!=$montantfraisecheance)$message.="<br>Inégalité montant frais echeance";
	}
	elseif($row_rs_tarif['Type_tarif']=='M')
	{ 
		
		$montantfraisecheance=ceil($row_rs_tarif['Montant_tarif_base']* $_SESSION['tarif']);	
		//if($_POST['fraisecheance']!=$montantfraisecheance)$message.="<br>Inégalité montant frais echeance";
	}
    $_SESSION['montantcotisation']=$montantcotisation;
	$_SESSION['montantfraisecheance']=$montantfraisecheance;
	if( isset($_POST['MM_insert']) && $_POST['MM_insert']=='form1' && $bool)
	{
		mysql_query("START TRANSACTION"); 
        $verif = true ;
        $zSql  = " UPDATE contrat SET " ;
        $zSql .= " Mode_paiement='" . $_POST['modepaiement'] . "'" ;
        $zSql .= ($_POST['modepaiement'] != MODE_PAIEMENT_PRELEVEMENT) ? ", Identifiant_RIB=NULL" : "" ;
        $zSql .= " WHERE Identifiant_contrat='" . $iIdentifiantContrat . "'" ;
        
        $req = mysql_query($zSql)or die(mysql_error());

        if(!$req) 
        { 
            mysql_query("rollback");
            $verif = false ;  
            $message.=colorer("Problème modification contrat (informations paiement), contactez la DSI ","false");
        }
        else
        {
            $_SESSION['modepaiement'] = $_POST['modepaiement'] ;
            mysql_query("commit") ;
            $message.=colorer("Le mode de paiement est modifi&eacute; ","true");
        }
    }

}


?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Paiement</title>
<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="css.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->

<script language="javascript">
// fonction pour le cas d'appui sur la touche entr�e
function valider() {
    document.form1.submit();
}
</script>
</head>

</head>

<body>
<div id="sf_menu1">
<h3>Production </h3>
<?php
	

	if(isset($message) && !empty($message)){
		echo $message;
	}
	if(isset($message1) && !empty($message1)){
		echo $message1;
	}
	
	/*if(isset($_POST['modepaiement'])){
		echo colorer("ok !!","false");
	}*/
	
	$nommodule="Modification administrative";
	//$nommodule2="Liste des adresse";
	$nom_module_saisie=" Paiement ";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<div class="cleared"></div>
</head>

<body>
<form id="form1" name="form1" method="post" action="index.php?Identifiant_personne=<?php echo $iIdentifiantPersonne ?>&Identifiant_contrat=<?php echo $iIdentifiantContrat ?>" onsubmit="return valider()">
 <div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td height="24">MODE DE PAIEMENT</td>
      <td width="298"><select name="modepaiement" id="modepaiement" onchange="" >
      <?php $requete=mysql_query("select elementdico.* from dictionnaire,elementdico where dictionnaire.Code_table='M0006' and dictionnaire.Identifiant_table=elementdico.Identifiant_table");
	 
	  while($reponse=mysql_fetch_array($requete)) {?>
        <option <?php if(isset($_SESSION['modepaiement'])) echo remind_select1('modepaiement',$reponse["Code_element"]); else  echo remind_select('modepaiement',$reponse["Code_element"]);?> value="<?php echo $reponse["Code_element"];?>"><?php echo $reponse["Nom_element"];?></option>
        <?php }?>
        
        
	      
   <div class="cleared"></div>
  <input type="hidden" name="MM_insert" value="form1" />
  <input type="hidden" name="Identifiant_personne" value="<?php echo $iIdentifiantPersonne ; ?>" />
        </select>      </td>
    </tr>
    <tr>
      <td height="24">MONTANT DE COTISATION</td>
      <td><label>
        <input   name="montantcotis" type="text" disabled="disabled" class="resizeall" id="montantcotis" value="<?php if(isset($_SESSION["montantcotisation"])) echo $_SESSION["montantcotisation"]?>" maxlength="10" />
      </label></td>
    </tr>
    <tr>
      <td height="24">DONT FRAIS ECHEANCE</td>
      <td> <input name="fraisecheance" disabled="disabled" type="text" class="resizeall" id="fraisecheance"  value="<?php if(isset($_SESSION["montantfraisecheance"])) echo $_SESSION["montantfraisecheance"]?>" maxlength="10" /></td>
    </tr>
  </table>
  
  </div></div>
    

<div class="imgright"></div>				
<div class="bt1"> <a href="refexterne/index.php<?php if(!empty($_SERVER['QUERY_STRING'])) echo "?".$_SERVER['QUERY_STRING']?>"><span class="btleft"></span>
        <span class="btmiddle">Référence externe</span>
        <span class="btright"></span>
    </a></div> 
   
   
   <?php 
   $zLinkRib = 'javascript:;' ;
   if((array_key_exists('modepaiement', $_POST) && $_POST['modepaiement']=="P")  || $_SESSION['modepaiement']=="P")
   {
       $zLinkRib = 'rib/index.php?Identifiant_personne=' . $iIdentifiantPersonne . '&Identifiant_contrat=' . $iIdentifiantContrat ;
   }
   ?>
    <div class="bt"> <a href="<?php echo $zLinkRib ; ?>"><span class="btleft"></span>
        <span class="btmiddle">RIB</span>
        <span class="btright"></span>
    </a></div> 
   <div class="bt"> <a href="../index.php?Numero_contrat=<?php echo $_SESSION['num_contrat']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div>  
	<div class="bt"> <a href="../index.php?Numero_contrat=<?php echo $_SESSION['num_contrat']; ?>&retour=0<?php if(isset($ok) && $ok==1 && isset($_SESSION['modepaiement']) && $_SESSION['modepaiement']=="P")echo "?rib=1" ?>"><span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a></div>  
	 <?php /*  if(isset($id) && $id==1 || (isset($_GET['rib']) && $_GET['rib'])){?>   
      <div class="bt">
	<span class="btleft"></span><a href=""></span>
	 <span class="btmiddle">Valider</span>
     <span class="btright"></span></a></div>  
      
   <?php } else	 { ?> <div class="bt">
   <span class="btleft"></span><input type="submit"  value="Valider"   class="resizeall btmiddle" />
    <span class="btright"></span></div>
    <?php } */ ?>  
	<div class="bt">
   <span class="btleft"></span><input type="submit"  value="Valider"   class="resizeall btmiddle" />
    <span class="btright"></span></div>
</form> </div>



</body>
</html>
<?php
mysql_free_result($rs_tarif);
?>
	
<?php ob_end_flush();?>